<template>
  <div id="role">
    <Loading v-if="roleList == null"></Loading>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">角色管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button
            class="mb10"
            type="primary"
            @click="isShowRoleDialog = true"
            >添加角色</el-button
          >
        </el-col>
      </el-row>

      <!-- 角色列表 -->
      <el-table :data="roleList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名" prop="rolename"></el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editRole(scope.row._id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteRole(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/修改角色对话框 -->
    <el-dialog
      :title="roleForm._id ? '修改角色信息' : '添加新角色'"
      v-model="isShowRoleDialog"
      width="400px"
      @close="roleDialogClose"
    >
      <el-form :model="roleForm" label-width="70px" ref="roleFormRef">
        <el-form-item label="角色名" prop="rolename">
          <el-input v-model="roleForm.rolename"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isShowRoleDialog = false">取 消</el-button>
          <el-button type="primary" @click="saveRole">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from "@vue/reactivity";
import { postData, getData, putData, deleteData } from "../../services/http";
import { getCurrentInstance, onBeforeMount } from "@vue/runtime-core";
export default {
  name: "Role",
  setup() {
    const { proxy } = getCurrentInstance();
    const state = reactive({
      roleForm: {},
      roleList: null,
    });
    const isShowRoleDialog = ref(false);
    const stateAsRefs = toRefs(state);
    const roleFormRef = ref(null);
    const getroleList = async () => {
      const { data: res } = await getData("rest/roles");
      state.roleList = res.data;
    };
    const saveRole = async () => {
      const id = state.roleForm._id;
      if (id) {
        await putData(`rest/roles/${id}`, state.roleForm, "修改成功");
      } else {
        await postData(
          "rest/roles",
          state.roleForm,
          `已成功添加角色:${state.roleForm.rolename}`
        );
      }
      getroleList();
      isShowRoleDialog.value = false;
    };
    const roleDialogClose = () => {
      roleFormRef.value.resetFields();
    };
    const editRole = async (id) => {
      const res = await getData(`rest/roles/${id}`);
      state.roleForm = res.data;
      isShowRoleDialog.value = true;
    };
    const deleteRole = (row) => {
      const { rolename, _id } = row;
      proxy
        .$confirm(`此操作将永久删除角色 ${rolename}, 是否继续?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async () => {
          await deleteData(`rest/roles/${_id}`, `已成功删除角色 ${rolename}`);
          getroleList();
        })
        .catch(() => {
          proxy.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    };
    onBeforeMount(() => {
      getroleList();
    });
    return {
      ...stateAsRefs,
      roleFormRef,
      saveRole,
      deleteRole,
      roleDialogClose,
      editRole,
      isShowRoleDialog,
    };
  },
};
</script>
